/*
 * @Author: sxb
 * @Email: sxb@mail.com
 * @Date: 2020-03-11 14:34:22
 * @Descripttion: 
 */
import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
import { INSPECT_MAX_BYTES } from 'buffer';
import { NzMessageService } from 'ng-zorro-antd/message';
import { BoxingManageService } from './boxing-manage.service';
import { FormBuilder, FormControl, FormGroup, Validators, ValidatorFn, AbstractControl } from '@angular/forms';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
  selector: 'app-boxing-manage',
  templateUrl: './boxing-manage.component.html',
  styleUrls: ['./boxing-manage.component.scss']
})
export class BoxingManageComponent implements OnInit {
  taskinfoRoles = {};
  boxingRoles = {};
  boxingBucketDetailRoles = {};

// 任务单列表start
  txtTaskCode = "";
  txtSaleCode = "";
  txtStockOutCode = "";
  txtMaterialCode = "";
  txtRecenterprise = "";
  selStatus = "";
  txtTaskCreStartData = "";
  txtTaskCreEndData = "";

  checkedTaskItem = null;

  taskPageIndex = 0;
  taskPageSize = 10;
  taskTotalSize = 100;
  taskListData = [];
  taskClickPage(index) {
    this.taskPageIndex = index - 1
  }
  taskSearch(){
    this.taskPageIndex = 0;
    this.getTaskInfo();
  }
  checkTaskSingle(data){
    this.taskListData.forEach(item => {
      if(item.id !== data.id){ item.checked = false; }
    })
    this.checkedTaskItem = data.checked ? data : null;
    if(this.checkedTaskItem != null){
      this.pageIndex = 0;
      this.getBoxingDetail(data.id);
    }else{
      this.listData = [];
    }
    this.detailListData = [];
  }

  getTaskInfo(){
    let data = {
      taskcode: this.txtTaskCode,
      salecode: this.txtSaleCode,
      stockoutcode: this.txtStockOutCode,
      materialCode: this.txtMaterialCode,
      recenterprise: this.txtRecenterprise,
      status: this.selStatus,
      startDate: this.txtTaskCreStartData,
      endDate: this.txtTaskCreEndData
    };
    this.boxingService.getTaskInfo(data).subscribe(res => {
      res.content.forEach(item => {
      item.checked = false;
      })
      this.taskTotalSize = res.totalElements
      this.taskListData = res.content;
    })
  }
  selTaskStatus(data){
    this.selStatus = data;
    this.taskSearch();
  }
  onTaskDateChange(date) {
    this.txtTaskCreStartData = this.formatDate(date[0]) != null ? this.formatDate(date[0]) : '';
    this.txtTaskCreEndData = this.formatDate(date[1]) != null ? this.formatDate(date[1]) : '';
    this.taskSearch();
  }
// 任务单列表end

// 装箱单列表start

  checkedTaskDetailItem = null;

  pageIndex = 0;
  pageSize = 10;
  totalSize = 100;
  clickPage(index) {
    this.pageIndex = index - 1
    this.getBoxingDetail(this.checkedTaskItem.id);
  }
  listData = [
    { id: '11', checked: false }, 
    { id: '223', checked: false }
  ];
  checkBoxDetailSingle(data){
    this.listData.forEach(item => {
      if(item.id !== data.id){ item.checked = false; }
    })
    this.checkedTaskDetailItem = data.checked ? data : null;
    this.getBoxingBucketDetail(data.id);
    this.detailListData = [];
  }

  getBoxingDetail(foid){
    let data = {foid:foid, page: this.pageIndex, size: this.pageSize};
    this.boxingService.getBoxingDetail(data).subscribe(res => {
      res.content.forEach(item => {
      item.checked = false;
      })
      this.totalSize = res.totalElements
      this.listData = res.content;
    })
  }
// 装箱单列表end

// 装箱单详细信息start
  detailPageIndex = 0;
  detailPageSize = 10;
  detailTotalSize = 100;
  detailListData = [];
  detailClickPage(index) {
    this.detailPageIndex = index - 1;
    this.getBoxingBucketDetail(this.checkedTaskDetailItem.id);
  }

  getBoxingBucketDetail(foid){
    let data = {
      foid:foid, 
      page: this.detailPageIndex, 
      size: this.detailPageSize,
      sort:"bucketnum,asc"};
    this.boxingService.getBoxingBucketDetail(data).subscribe(res => {
      res.content.forEach(item => {
      item.checked = false;
      })
      this.detailTotalSize = res.totalElements
      this.detailListData = res.content;
    })
  }
// 装箱单详细信息end

// 创建装箱单start

  validateBoxingForm: FormGroup;

  visBoxingRCPT:Boolean = false;
  maxKey:number = 1;
  boxingRCPTData:any = [];
  txtReptRecenterprise: String = '';
  txtReptMaterialCode: String = '';
  txtReptMaterialName: String = '';
  txtBoxCode: String = '';

  addBoxingRCPT(){
    for (const i in this.validateBoxingForm.controls) {
      this.validateBoxingForm.controls[i].markAsDirty();
      this.validateBoxingForm.controls[i].updateValueAndValidity();
    }
    if (this.validateBoxingForm.status == "VALID") {
      let boxType = this.validateBoxingForm.get('txtBoxType').value;
      let boxDate = this.validateBoxingForm.get('txtBoxDate').value;
      let boxPerson = this.validateBoxingForm.get('txtBoxPerson').value;
      let boxCode = this.validateBoxingForm.get('txtBoxCode').value;
      let data = {
        key: this.maxKey,
        MaterialCode: this.validateBoxingForm.get("txtReptMaterialCode").value,
        MaterialName: this.validateBoxingForm.get("txtReptMaterialName").value,
        BoxType: boxType,
        BoxDate: this.formatDate(boxDate),
        BoxPerson: boxPerson,
        BoxCode: boxCode
      }
      this.boxingRCPTData = [ ...this.boxingRCPTData, data];
      this.maxKey = this.maxKey + 1;
    }
  }
  delBoxingRCPT(data){   
    this.boxingRCPTData = this.boxingRCPTData.filter(d => d.key !== data.key);
  }
  ShowBoxingRCPT(){
    if (this.checkedTaskItem == null) {
      this.message.warning("请选择一条任务单信息！");
    }else{
      this.visBoxingRCPT = true;
      let chkTaskItem = this.checkedTaskItem;
      this.validateBoxingForm.reset();  
      this.validateBoxingForm.patchValue({
        txtReptRecenterprise: chkTaskItem.recenterprise,
        txtReptMaterialCode: chkTaskItem.materialCode,
        txtReptMaterialName: chkTaskItem.materialName
      })
      this.boxingRCPTData = [];
    }
  }
  BoxingRCPTCancel(){
    this.visBoxingRCPT = false;
  }
  BoxingRCPTOk(){
    for (const i in this.validateBoxingForm.controls) {
      this.validateBoxingForm.controls[i].markAsDirty();
      this.validateBoxingForm.controls[i].updateValueAndValidity();
    }
    let _lstBoxCode = [];
    this.boxingRCPTData.forEach(item => {
      _lstBoxCode.push(item.BoxCode);
    });
    let data = {
      foid: this.checkedTaskItem.id,
      boxtype: this.validateBoxingForm.get("txtBoxType").value,
      boxdate: this.formatDate(this.validateBoxingForm.get("txtBoxDate").value),
      boxperson: this.validateBoxingForm.get("txtBoxPerson").value,
      lstBoxCode: _lstBoxCode
    };
    this.boxingService.postCreateList(data).subscribe(res => {
      this.visBoxingRCPT = false;
      this.getTaskInfo();
    })
  }
// 创建装箱单end

// 打包装箱strat

  validatePackBoxingForm: FormGroup;
  
  visPackBoxing:Boolean = false;
  packMaxKey:number = 1;
  packBoxingData = [];
  addPackBoxingRCPT(){
    for (const i in this.validatePackBoxingForm.controls) {
      this.validatePackBoxingForm.controls[i].markAsDirty();
      this.validatePackBoxingForm.controls[i].updateValueAndValidity();
    }
    if (this.validatePackBoxingForm.status == "VALID") {
      let data = {
        key: this.packMaxKey,
        MaterialCode: this.validatePackBoxingForm.get("txtPackBoxMaterialCode").value,
        MaterialName: this.validatePackBoxingForm.get("txtPackBoxMaterialName").value,
        BoxBatch: this.validatePackBoxingForm.get("txtBoxBatch").value,
        BoxWeight: this.validatePackBoxingForm.get("txtBoxWeight").value,
        BoxBucketNum: this.validatePackBoxingForm.get("txtBoxBucketNum").value
      }
      this.packBoxingData = [ ...this.packBoxingData, data];
      this.packMaxKey = this.packMaxKey + 1;
    }
  }
  delPackBoxing(data){   
    this.packBoxingData = this.packBoxingData.filter(d => d.key !== data.key);
  }
  ShowPackBoxing(){
    if (this.checkedTaskItem == null || this.checkedTaskDetailItem == null) {
      this.message.warning("请选择任务单和装箱单信息！");
    }else{
      this.visPackBoxing = true;
      let chkTaskItem = this.checkedTaskItem;
      this.validatePackBoxingForm.reset();  
      this.validatePackBoxingForm.patchValue({
        txtPackBoxMaterialCode: chkTaskItem.materialCode,
        txtPackBoxMaterialName: chkTaskItem.materialName
      })
      this.packBoxingData = [];
    }
  }
  PackBoxingCancel(){    
    this.visPackBoxing = false;
  }
  PackBoxingOk(){
    for (const i in this.validatePackBoxingForm.controls) {
      this.validatePackBoxingForm.controls[i].markAsDirty();
      this.validatePackBoxingForm.controls[i].updateValueAndValidity();
    }
    if (this.validatePackBoxingForm.status == "VALID") {
      let BoxingBucketDetail = [];
      this.packBoxingData.forEach(item => {
        BoxingBucketDetail.push({
          bucketnum: item.BoxBucketNum,
          foid: this.checkedTaskDetailItem.id,
          productBatch: item.BoxBatch,
          weight: item.BoxWeight
        });
      });
      this.boxingService.postBoxingBucketDetailCreate(BoxingBucketDetail).subscribe(res => {
        this.visPackBoxing = false;
        this.getBoxingBucketDetail(this.checkedTaskDetailItem.id);
      })
    }
  }
// 打包装箱end

  // 时间格式化
  formatDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd HH:mm:ss');
  }

  constructor(
    private message: NzMessageService,
    private datePipe: DatePipe,
    private fb: FormBuilder,
    private commonService : HCommonService,
    private boxingService : BoxingManageService
    ) { }

  ngOnInit() {
    /* 获取权限 */
    this.taskinfoRoles = this.commonService.getRoles("taskinfo");
    this.boxingRoles = this.commonService.getRoles("boxing");
    this.boxingBucketDetailRoles = this.commonService.getRoles("boxingBucketDetail");

    this.validateBoxingForm = this.fb.group({
      txtReptRecenterprise:[null],
      txtReptMaterialCode:[null],
      txtReptMaterialName:[null],
      txtBoxType: [null, [Validators.required]],
      txtBoxDate: [null, [Validators.required]],
      txtBoxPerson: [null, [Validators.required]],
      txtBoxCode:[null, [Validators.required]]
    })
    this.validatePackBoxingForm = this.fb.group({
      txtPackBoxMaterialCode:[null],
      txtPackBoxMaterialName:[null],
      txtBoxBatch: [null, [Validators.required]],// 产品批次
      txtBoxBucketNum: [null, [Validators.required]],// 桶号
      txtBoxWeight: [null, [Validators.required]]// 重量
    })

    this.getTaskInfo();
    this.getBoxingDetail(0);
  }

}
